package cn.iocoder.yudao.module.erp.controller.admin.dp.dpmsk;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpbisk.vo.DpBiskSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpmsk.vo.DpMskPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpmsk.vo.DpMskRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpmsk.vo.DpMskSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpwsl.vo.DpWslPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpwsl.vo.DpWslRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dispatchvehicle.DispatchVehicleDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpMskDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpWslDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemMskDO;
import cn.iocoder.yudao.module.erp.service.dp.DpMskService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - ERP莫斯科仓库")
@RestController
@RequestMapping("/erp/dp-msk")
@Validated
public class DpMskController {

    @Resource
    private DpMskService dpMskService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP莫斯科仓库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:create')")
    public CommonResult<String> createDpMsk(@Valid @RequestBody DpMskSaveReqVO createReqVO) {
        return success(dpMskService.createDpMsk(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP莫斯科仓库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:update')")
    public CommonResult<Boolean> updateDpMsk(@Valid @RequestBody DpMskSaveReqVO updateReqVO) {
        dpMskService.updateDpMsk(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP莫斯科仓库")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:delete')")
    public CommonResult<Boolean> deleteDpMsk(@RequestParam("id") String id) {
        dpMskService.deleteDpMsk(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP莫斯科仓库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:query')")
    public CommonResult<DpMskRespVO> getDpMsk(@RequestParam("id") String id) {
        DpMskDO dpMsk = dpMskService.getDpMsk(id);
        return success(BeanUtils.toBean(dpMsk, DpMskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP莫斯科仓库分页")
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:query')")
    public CommonResult<PageResult<DpMskRespVO>> getDpMskPage(@Valid DpMskPageReqVO pageReqVO) {
        PageResult<DpMskDO> pageResult = dpMskService.getDpMskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpMskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP莫斯科仓库 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpMskExcel(@Valid DpMskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpMskDO> list = dpMskService.getDpMskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP莫斯科仓库.xls", "数据", DpMskRespVO.class,
                        BeanUtils.toBean(list, DpMskRespVO.class));
    }

    @GetMapping("/dpMskOuterList")
    @Operation(summary = "获得ERP莫斯科制单出库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:query')")
    public CommonResult<PageResult<DpMskRespVO>> dpMskOuterList(@Valid DpMskPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<DpMskDO> dpMskOuterList = dpMskService.dpMskOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpMskOuterList, DpMskRespVO.class));
    }

    @PostMapping("/dpMskOutStroe")
    @Operation(summary = "ERP莫斯科仓库单据配车出库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:dpmskoutstroe')")
    public CommonResult<Boolean> dpWslOutStroe(@Valid @RequestBody DispatchVehicleSaveReqVO createReqVO) {
        dpMskService.dpMskOutStroe(createReqVO);
        return success(true);
    }

    // ==================== 子表（ERP莫斯科制单货号详情） ====================

    @GetMapping("/dp-item-msk/list-by-no")
    @Operation(summary = "获得ERP莫斯科制单货号详情列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:detail')")
    public CommonResult<List<DpItemMskDO>> getDpItemMskListByNo(@RequestParam("no") String no) {
        return success(dpMskService.getDpItemMskListByNo(no));
    }

    @PostMapping("/enterStroeDpMsk")
    @Operation(summary = "ERP莫斯科运单入库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:enter')")
    public CommonResult<Boolean> enterStroeDpMsk(@Valid @RequestBody List<DispatchVehicleSaveReqVO> createReqVO) {
        dpMskService.enterStroeDpMsk(createReqVO);
        return success(true);
    }

    @GetMapping("/dpMskEnterList")
    @Operation(summary = "获得ERP莫斯科制单入库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:enter')")
    public CommonResult<List<DispatchVehicleRespVO>> dpMskEnterList(@RequestParam(value = "dispatchNumber",required = false) String dispatchNumber,
                                                                     @RequestParam(value = "line",required = false) Integer line,
                                                                     @RequestParam(value = "dest",required = false) Integer dest,
                                                                    @RequestParam(value = "dpId",required = false) String dpId) {
        List<DispatchVehicleDO> dpMskEnterList = dpMskService.dpMskEnterList(dispatchNumber,line,1, dest,dpId);
        return success(BeanUtils.toBean(dpMskEnterList, DispatchVehicleRespVO.class));
    }

    @PostMapping("/signDp")
    @Operation(summary = "ERP到货签收")
//    @PreAuthorize("@ss.hasPermission('erp:dp-msk:enter')")
    public CommonResult<Boolean> signDp(@Valid @RequestBody DpMskSaveReqVO updateReqVO) {
        dpMskService.signDp(updateReqVO);
        return success(true);
    }

}